//
//  FLLRecommendTableViewCell.m
//  FLLGodOfCookery
//
//  Created by LL.F on 16/8/30.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLRecommendTableViewCell.h"
#import "FLLThreeItemCollectionViewCell.h"
#import "FLLMacro.h"
#import "UIImageView+WebCache.h"
#import "Widget_Data.h"


@interface FLLRecommendTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>


@end

@implementation FLLRecommendTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    
    self.threeItemLayout.minimumInteritemSpacing = CellMargin;
    self.threeItemLayout.minimumLineSpacing = 0;
    self.threeItemLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.threeItemCollectionView.delegate = self;
    self.threeItemCollectionView.dataSource = self;
        self.threeItemCollectionView.bounces = NO;
    self.threeItemCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.threeItemCollectionView registerNib:[UINib nibWithNibName:@"FLLThreeItemCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"threeItemCollectionCell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_todayImageArray.count != 0) {
        return 3;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    FLLThreeItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"threeItemCollectionCell" forIndexPath:indexPath];
    // 给图片赋值
    [cell.dishPictureImageView sd_setImageWithURL:[NSURL URLWithString:[_todayImageArray[indexPath.row] valueForKey:@"image"]] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    // 给title赋值
    cell.dishNameLabel.text = [self.todayTitltArray[indexPath.row] valueForKey:@"tilteText"];
    cell.dishNameLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];
    cell.dishIntroductionLabel.text = [self.contentArray[indexPath.row] valueForKey:@"contentText"];
    cell.dishIntroductionLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    // 跳到视频界面
    [cell.videoButton addTarget:self action:@selector(didClickedVideoButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.videoButton.tag = indexPath.row;

    
    

    return cell;
}
- (void)didClickedVideoButton:(UIButton *)button
{

    NSInteger index = button.tag;
    NSString *videoURL = self.todayVideoArray[index][@"video"];
    self.videoBlock(videoURL);
    

}
- (void)setTodayImageArray:(NSArray *)todayImageArray
{

    _todayImageArray = todayImageArray;
    [self.threeItemCollectionView reloadData];
}
- (void)setTodayTitltArray:(NSArray *)todayTitltArray
{

    _todayTitltArray = todayTitltArray;
    [self.threeItemCollectionView reloadData];

}
- (void)setContentArray:(NSArray *)contentArray
{

    _contentArray = contentArray;
    [self.threeItemCollectionView reloadData];
}
- (void)setTodayIDArray:(NSArray *)todayIDArray
{

    _todayIDArray = todayIDArray;
    [self.threeItemCollectionView reloadData];
    
}
- (void)setTodayVideoArray:(NSMutableArray *)todayVideoArray
{

    _todayVideoArray = todayVideoArray;
    [self.threeItemCollectionView reloadData];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = (_threeItemCollectionView.bounds.size.width - 2 * CellMargin) / 3.0;
    CGFloat itemHeight = _threeItemCollectionView.bounds.size.height ;
    return CGSizeMake(itemWidth, itemHeight);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    self.idBlock(_todayIDArray[indexPath.row][@"link"]);
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
